package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsProduct;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsProductMapper;
import com.caiyi.financial.nirvana.sms.mapper.SmsUserMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.QueryProductBean;
import com.caiyi.financial.nirvana.sms.vo.SmsProductByParamVo;
import com.caiyi.financial.nirvana.sms.vo.SmsUserProductListVo;
import com.youyu.common.api.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SmsProductService extends BaseService<SmsProduct> {

    @Autowired
    private SmsProductMapper smsProductMapper;
    @Autowired
    private SmsUserMapper smsUserMapper;

    public int addProduct(SmsProduct smsProduct) {
        int insertCount = this.insertByEntitySelective(smsProduct);
        //redis中添加产品
        String key = String.format(JedisKey.SMS_PRODUCT, smsProduct.getId());
        JedisService.set(key, JSON.toJSONString(smsProduct));
        return insertCount;
    }

    public int deleteProduct(Integer id) {
        SmsProduct smsProduct = new SmsProduct();
        smsProduct.setId(id);
        int deleteCount = this.deleteByPrimaryKey(smsProduct);
        //redis中删除Product
        String key = String.format(JedisKey.SMS_PRODUCT, id);
        JedisService.del(key);
        return deleteCount;
    }

    public int updateProduct(SmsProduct smsProduct) {
        int updateCount = this.updateByEntitySelective(smsProduct);
        //redis中更新Product
        String key = String.format(JedisKey.SMS_PRODUCT, smsProduct.getId());
        JedisService.set(key, JSON.toJSONString(smsProduct));
        logger.info("updateByProductId id {}", smsProduct.getId());
        return updateCount;
    }

    public PageBean<SmsProductByParamVo> getProductByParams(QueryProductBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsProductByParamVo> maps = smsProductMapper.selectProductListByParams(bean);
        PageBean<SmsProductByParamVo> pageBean = new PageBean<>(maps);
        return pageBean;
    }

    public List<SmsUserProductListVo> getUpdateUserProductParams() {
        List<SmsUserProductListVo> maps = smsProductMapper.getUpdateUserProductParams();
        return maps;
    }

    /**
     * 配置客户产品，更新数据库中account的productId为id值
     */
    public int updateUserProduct(Integer id, String account) {
        int updateCount = smsProductMapper.updateUserProduct(id, account);
        SmsUser user = smsUserMapper.selectByAccount(account);
        //更新redis中account用户的productId
        String key = String.format(JedisKey.SMS_USER, account);
        JedisService.set(key, JSON.toJSONString(user));
        return updateCount;
    }

}
